package com.liuxu.topnews.utils;

import android.os.Environment;
import android.util.Log;
import android.util.Xml;

import com.liuxu.topnews.MyApplication;
import com.liuxu.topnews.bean.App;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class SettingUtils {
    public static SettingUtils Instance;
    public static SettingUtils getInstance() {
        if (Instance == null) {
            Instance = new SettingUtils();
        }
        return Instance;
    }

    public List<App> GetAppInfoXml(){
        List<App> appinfos=new ArrayList<App>();
        XmlPullParser xpp = Xml.newPullParser();
        App app = null;
        try {
            InputStream inputStream = MyApplication.assetManager.open("app_info.xml");
            xpp.setInput(inputStream, "utf-8");
            int type = xpp.getEventType();
            while (type != XmlPullParser.END_DOCUMENT) {
                String currentTagName = xpp.getName();
                switch (type) {
                    case XmlPullParser.START_TAG:
                        if (currentTagName.equals("app")) {
                             app = new App();
                        }else if (currentTagName.equals("id")) {
                            app.setId(xpp.nextText());
                        }else if (currentTagName.equals("name")) {
                            app.setEname(xpp.nextText());
                        }else if (currentTagName.equals("packname")) {
                            app.setPackname(xpp.nextText());
                        }else if (currentTagName.equals("zname")) {
                            app.setZname(xpp.nextText());
                        }else if (currentTagName.equals("hot")) {
                            app.setHot(xpp.nextText().equals("true"));
                        }else if (currentTagName.equals("mainactivity")) {
                            app.setMainname(xpp.nextText());
                        }else if (currentTagName.equals("newsacitvity")) {
                            app.setNewsname(xpp.nextText());
                        }else if (currentTagName.equals("videoactivity")) {
                            app.setVideoActivity(xpp.nextText());
                        }else if (currentTagName.equals("imageactivity")) {
                            app.setImageactivity(xpp.nextText());
                        }else if (currentTagName.equals("FlagText1")) {
                            app.setFlagText1(xpp.nextText());
                        }else if (currentTagName.equals("FlagText2")) {
                            app.setFlagText2(xpp.nextText());
                        }else if (currentTagName.equals("FlagId1")) {
                            app.setFlagId1(xpp.nextText());
                        }else if (currentTagName.equals("FlagId2")) {
                            app.setFlagId2(xpp.nextText());
                        }else if (currentTagName.equals("backactivity1")) {
                            app.setBackactivity1(xpp.nextText());
                        }else if (currentTagName.equals("backactivity2")) {
                            app.setBackactivity2(xpp.nextText());
                        }else if (currentTagName.equals("backactivity3")) {
                            app.setBackactivity3(xpp.nextText());
                        }else if (currentTagName.equals("backactivity4")) {
                            app.setBackactivity4(xpp.nextText());
                        }else if (currentTagName.equals("quickup")) {
                            app.setQuickup(Integer.parseInt(xpp.nextText()));
                        }else if (currentTagName.equals("quickdown")) {
                            app.setQuickdown(Integer.parseInt(xpp.nextText()));
                        }else if (currentTagName.equals("click")) {
                            app.setClickBack(xpp.nextText().equals("true"));
                        }else if (currentTagName.equals("pointx")) {
                            app.setPointx(Integer.parseInt(xpp.nextText()));
                        }else if (currentTagName.equals("pointy")) {
                            app.setPointy(Integer.parseInt(xpp.nextText()));
                        } else if (currentTagName.equals("jsversion")) {
                            app.setJsversion(xpp.nextText());
                        }else if (currentTagName.equals("getmoney")) {
                            app.setGetMoney(xpp.nextText());
                        }
                        break;
                    case XmlPullParser.END_TAG:
                        if (currentTagName.equals("app")) {
                            appinfos.add(app);
                        }
                        break;
                    default:
                        break;
                }
                type = xpp.next();
            }
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return appinfos;
    }

    public List<App> GetAppSetXmlByboolean(boolean defaultSetting){
        InputStream inputStream=null;
        try {
            if (defaultSetting&fileIsExists()) {
                inputStream = MyApplication.getContext().openFileInput("read_set.xml");

                Log.v("配置文件", "私有");
            } else {
                inputStream = MyApplication.assetManager.open("read_set.xml");
                Log.v("配置文件", "默认");
            }
        }catch (IOException e) {
                e.printStackTrace();
            }
        List<App> apps=GetAppSetXmlByIs(inputStream);
        return apps;
    }
    public List<App> GetAppSetXmlByPath(String path){
        Log.v("通过路径导入","配置文件");
        File file = new File(path);
        InputStream inputStream=null;
        try {
            inputStream = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        List<App> apps=GetAppSetXmlByIs(inputStream);
        return apps;
    }
    public ArrayList<App> GetAppSetXmlByIs(InputStream inputStream){
        XmlPullParser xpp = Xml.newPullParser();
        ArrayList<App> apps = new ArrayList<App>();
        App app = null;
        try {
            xpp.setInput(inputStream, "utf-8");
            int type = xpp.getEventType();
            while (type != XmlPullParser.END_DOCUMENT) {
                String currentTagName = xpp.getName();
                switch (type) {
                    case XmlPullParser.START_TAG:
                        if (currentTagName.equals("app")) {
                            app = new App();
                        } else if (currentTagName.equals("id")) {
                            app.setId(xpp.nextText());
                        } else if (currentTagName.equals("name")) {
                            app.setEname(xpp.nextText());
                        } else if (currentTagName.equals("movetype")) {
                            app.setMovetype(Integer.parseInt(xpp.nextText()));
                        } else if (currentTagName.equals("readtype")) {
                            app.setReadtype(Integer.parseInt(xpp.nextText()));
                        }  else if (currentTagName.equals("uptime")) {
                            app.setUptime(Integer.parseInt(xpp.nextText()));
                        } else if (currentTagName.equals("downtime")) {
                            app.setDowntime(Integer.parseInt(xpp.nextText()));
                        } else if (currentTagName.equals("pagenumber")) {
                            app.setPagenumber(Integer.parseInt(xpp.nextText()));
                        }else if (currentTagName.equals("max_read")) {
                            app.setMax_read(Integer.parseInt(xpp.nextText()));
                        }
                        break;
                    case XmlPullParser.END_TAG:
                        if (currentTagName.equals("app")) {
                            apps.add(app);
                        }
                        break;
                    default:
                        break;
                }
                type = xpp.next();
            }
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return apps;

    }
    public void saveSetting(List<App> apps,boolean selfspace){
        StringBuffer sb = new StringBuffer();
        //封装一个声明头
        sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
        //封装根节点
        sb.append("<apps>");
        //循环遍历list集合封装所有的短信
        for (App app : apps) {
            sb.append("<app>");
            sb.append("<id>");
            sb.append(app.getId());
            sb.append("</id>");
            sb.append("<name>");
            sb.append(app.getEname());
            sb.append("</name>");
            sb.append("<movetype>");
            sb.append(app.getMovetype());
            sb.append("</movetype>");
            sb.append("<readtype>");
            sb.append(app.getReadtype());
            sb.append("</readtype>");
            sb.append("<uptime>");
            sb.append(app.getUptime());
            sb.append("</uptime>");
            sb.append("<downtime>");
            sb.append(app.getDowntime());
            sb.append("</downtime>");
            sb.append("<pagenumber>");
            sb.append(app.getPagenumber());
            sb.append("</pagenumber>");
            sb.append("<max_read>");
            sb.append(app.getMax_read());
            sb.append("</max_read>");
            sb.append("</app>");
        }
        sb.append("</apps>");
        FileOutputStream openFileOutput  = null;
        try{
            //将stringBuffer中的xml字符串写入私有目录中的文件
            if (selfspace){
                openFileOutput = MyApplication.getContext().openFileOutput("read_set.xml", MyApplication.getContext().MODE_PRIVATE);
            }else {
                openFileOutput = new FileOutputStream(Environment.getExternalStorageDirectory().getCanonicalPath() + "/read_set.xml");
            }
            openFileOutput.write(sb.toString().getBytes());
        }catch (Exception e) {
            e.printStackTrace();
        }finally{
            if(openFileOutput != null){
                try{
                    openFileOutput.close();
                }catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public boolean fileIsExists() {
        try
        {
            File f=new File(MyApplication.getContext().getFilesDir()+ "/read_set.xml");
            if(!f.exists())
            {
                return false;
            }
        }
        catch (Exception e)
        {
            return false;
        }
        return true;
    }

}
